using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iPodConverter;
using Teknologika.iPodConverter;
using System.Threading;

namespace iPodConverter.UserInterface
{
    public partial class Form1 : Form
    {
        private string inputFile;
        private string outputFile;
        private string outputFileTitle;
        private string targetResolution;
        private bool addToItunes;
        private ffmpeg ffmpegConverter;
 
        

  
        public Form1()
        {
            InitializeComponent();

            // Set the default values
            addToItunes = true;
            this.ckbAddToItunes.Checked = true;
            Form1.CheckForIllegalCrossThreadCalls = false;
            
           
        }

      
    

        private void btnSelectSourceFile_Click(object sender, EventArgs e)
        {
            dlgSelectSourceFile.Filter = "Microsoft Recorded TV Show (*.dvr-ms)|*.dvr-ms|Windows video file (*.avi;*.wmv)|*.avi;*.wmv|Any file (*.*)|*.*";
            this.dlgSelectSourceFile.ShowDialog();
        }

        private void btnSelectDestinationDirectory_Click(object sender, EventArgs e)
        {
            dlgSaveFile.Filter = "MPEG-4 (*.mp4)|*.mp4";
            dlgSaveFile.DefaultExt = ".mp4";
            dlgSaveFile.AddExtension = true;
            dlgSaveFile.ShowDialog();
        }

        private void dlgSelectSourceFile_FileOk(object sender, CancelEventArgs e)
        {
            inputFile = dlgSelectSourceFile.FileName;
            txtSourceFile.Text = inputFile;
            string[] splitOne = inputFile.Split(".".ToCharArray());
            string[] splitTwo = splitOne[0].Split("\\".ToCharArray());
            txtFileTitle.Text = splitTwo[splitTwo.Length - 1].ToString();
        }

        private void dlgSaveFile_FileOk(object sender, CancelEventArgs e)
        {
            outputFile = dlgSaveFile.FileName;
            txtOutputFile.Text = outputFile;
            
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            btnConvert.Enabled = false;
            btnCancel.Enabled = true;
            //progressBar1.Visible = true;

            if (inputFile == null || inputFile == "")
            {
                MessageBox.Show("Please select a file to convert.");
                return;
            }

            if (outputFile == null || outputFile == "")
            {
                MessageBox.Show("Please select an output file.");
                return;
            }

            if (outputFileTitle == null || outputFileTitle == "")
            {
                MessageBox.Show("Please add a name for the output file.");
                return;
            }

            if (rbIphone.Checked)
                targetResolution = DeviceResolutions.iPhone;
            if (rbIpodClassic.Checked)
                targetResolution = DeviceResolutions.iPodClassic;
            if (rbIpodNano.Checked)
                targetResolution = DeviceResolutions.iPodNano;
            if (rbIpodTouch.Checked)
                targetResolution = DeviceResolutions.iPodTouch;

            ffmpegConverter = new ffmpeg();
            ffmpegConverter.InputFileName = inputFile;
            ffmpegConverter.OutputFileName = outputFile;
            ffmpegConverter.VideoTitle = outputFileTitle;
            ffmpegConverter.TargetResolution = targetResolution;

            if (ckbShowConversionOutput.Checked == true)
            {
                ffmpegConverter.showFfmpegOutput = true;
            }
            else
            {
                ffmpegConverter.showFfmpegOutput = false;
            }

           
            
            
            ffmpegConverter.ConvertFile();

            Thread progressBarThread = new Thread(new ThreadStart(this.CheckConversionProgressOnAnotherThread));
            progressBarThread.Start();

            
            Thread iTunesWaitThread = new Thread(new ThreadStart(this.AddToItunesOnAnotherThread));
            iTunesWaitThread.Start();

            
        }

        private void AddToItunesOnAnotherThread()
        {
            while (ffmpegConverter.ffmpegProcess.HasExited == false)
            {
                System.Threading.Thread.Sleep(500);
            }

            if (addToItunes)
            {
                iTunes iTunesLoader = new iTunes();
                iTunesLoader.AddFile(outputFile);
                
            }

            // Reset the applicaiton UI back to before we started the conversion
            this.btnCancel.Enabled = false;
            this.btnConvert.Enabled = true;
            //this.progressBar1.Visible = false;
            //this.progressBar1.Value = 0;
          
        }

        private void CheckConversionProgressOnAnotherThread()
        {
            //while (ffmpegConverter.ffmpegProcess.HasExited == false)
            //{
            //    this.progressBar1.Value = ffmpegConverter.PercentComplete;
               
                
            //}

        }

        private void txtSourceFile_TextChanged(object sender, EventArgs e)
        {
            inputFile = txtSourceFile.Text;
        }

        private void txtOutputFile_TextChanged(object sender, EventArgs e)
        {
            outputFile = txtOutputFile.Text;
        }

        private void txtFileTitle_TextChanged(object sender, EventArgs e)
        {
            outputFileTitle = txtFileTitle.Text;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnCancel.Enabled = false;
            addToItunes = false;
            ffmpegConverter.CancelConversion();
            System.IO.File.Delete(outputFile);
            btnConvert.Enabled = true;

        }

        private void ckbAddToItunes_CheckedChanged(object sender, EventArgs e)
        {
           
                addToItunes = ckbAddToItunes.Checked;    
        }

       
       
       
           

  
 
        
      
    }
}